package sg.nus.iss.vmcs.gui.elements;

import java.awt.Panel;

import sg.nus.iss.vmcs.VMCSException;
import sg.nus.iss.vmcs.controllers.MaintenanceController;
import sg.nus.iss.vmcs.controllers.StoreController;
import sg.nus.iss.vmcs.guilisteners.CoinDisplayListener;
import sg.nus.iss.vmcs.utils.Constant;
import sg.nus.iss.vmcs.utils.StoreType;

/**
 * Coin quantity display in Maintenance Panel
 * Updated to use ButtonItemStoreViewerBuilder
 * @author Pang Pingli
 * @author Philip Chew
 */
public class CoinDisplay extends Panel {

	/** Auto Generated ID */
	private static final long serialVersionUID = 1850777776207662852L;
	
	private StoreController			storeCtrl;
	private MaintenanceController	mCtrl;
	private StoreViewer		        sv;

	/**
	 * Standard controller for the coin display, using Maintenance Controller as the UI's controller
	 * @param mctrl Maintenance Controller instance
	 */
	public CoinDisplay(MaintenanceController  mctrl) {
		mCtrl = mctrl;
		storeCtrl = mCtrl.getMainController().getStoreController();
		
		ButtonItemStoreViewerBuilder builder = new ButtonItemStoreViewerBuilder(
				Constant.MAINTAIN_COIN_TITLE, 
				new CoinDisplayListener(mCtrl), 
				storeCtrl.getStore(StoreType.CASH));
		
		builder.createNewStoreViewer();
		builder.buildBanner();
		builder.buildStoreViewerItems();
		builder.buildListeners();

		sv = builder.getStoreViewer();
		this.add(sv);
	}

	/**
	 * Sets the active or inactive state of the Button Item Display
	 * @param st true to set active, false for inactive
	 */
	public void setActive(boolean st){
		sv.setActive(st);
	}

	/**
	 * Sets the current quantity displayed in the ButtonItem Display that is contained by this class
	 * 
	 * @param idx Index of the button item to set
	 * @param qty Quantity to set to
	 * @throws VMCSException
	 */
	public void displayQty(int idx, int qty) throws VMCSException {
		if(sv.getActiveItem() != null) {
			sv.getActiveItem().setDisplay();
		}
		
		sv.getViewItems(idx).setQuantity(qty);
		sv.setActiveItem(sv.getViewItems(idx));
		sv.getActiveItem().setDisplay();
	}

	/**
	 * Gets the current displayed coin index. 
	 * This is used for updating when store MachinerySimulatorPanel is changed.
	 * 
	 * @return Current displayed coin index
	 */
	public int getCurIdx() {
		return sv.getActiveItem().getIndex();
	}
}